layui.use('form', function(){
    var form = layui.form;
    setTimeout(function (){
        form.render('select');
    }, 500);
    form.on('select(selectDepartment)', function(data){
        vm.$data.departmentId = data.value;
        $.ajax({
            type: "get",
            url: "/authUser/parentGroup/"+data.value,
            contentType: 'application/x-www-form-urlencoded',
            dataType: "json",
            async: true,
            data: {},
            success: function (result) {
                if (result.code == 500) {
                    layer.msg(result.msg, {
                        icon: 1
                    })
                } else {
                    vm.$data.GroupList = result.data
                    setTimeout(function (){
                        form.render('select');
                    }, 500);
                    vm.$data.userGroupId = result.data[0].userId;
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                if(xhr.status==403) {
                    alert("无权限")
                    location.reload([true])
                }
            }
        });
    });
    form.on('select(selectGroup)', function(data){
        vm.$data.userGroupId = data.value;
    });

});

var vm = new Vue({
    el: '#app',
    data: {
        userId:"",
        userName:"",
        userTrueName:"",
        userGroupId :"",
        departmentId :"",
        departmentList: [],
        GroupList:[],
    },
    created() {
        this.getUserDetail()
    },
    methods: {
        getUserDetail() {
            var userId = location.hash.substring(1)
            console.log(userId)
            $.ajax({
                type: "get",
                url: "/authUser/"+userId,
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: {},
                success: function (result) {
                    if (result.code == 500) {
                        alert(result.msg)
                    } else {
                        vm.userId= result.data[0].id
                        vm.userName = result.data[0].name
                        vm.userTrueName = result.data[0].userTrueName
                        vm.departmentId = result.data[0].userDepartment
                        vm.$options.methods.initList()
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                        location.reload([true])
                    }
                }
            });

        },
        initList() {
            $.ajax({
                type: "get",
                url: "/copDepartment/list",
                contentType: 'application/x-www-form-urlencoded',
                dataType: "json",
                async: true,
                data: {},
                success: function (result) {
                    if (result.code == 500) {
                        layer.msg(result.msg, {
                            icon: 1
                        })
                    } else {
                        vm.$data.departmentList = result.data
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                        location.reload([true])
                    }
                }
            });
            $.ajax({
                type: "get",
                url: "/authUser/parentGroup/"+vm.$data.departmentId ,
                contentType: 'application/x-www-form-urlencoded',
                dataType: "json",
                async: true,
                data: {},
                success: function (result) {
                    if (result.code == 500) {
                        layer.msg(result.msg, {
                            icon: 1
                        })
                    } else {
                        vm.$data.GroupList = result.data
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                        location.reload([true])
                    }
                }
            });
        },
        update() {
            var tmp = {
                "userId":vm.userId,
                "userGroup":vm.userGroupId,
                "userDepartment":vm.departmentId,
            };
            var data = JSON.stringify(tmp)
            $.ajax({
                type: "PUT",
                url: "/authUser",
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: data,
                success: function (result) {
                    if (result.code == 500) {
                        layer.msg(result.msg)
                    } else {
                        alert("修改成功")
                        window.location.href="userManger.html"
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                        location.reload([true])
                    }
                }
            });
        },
        back(){
            window.location.href="userManger.html"
        }
    }
})